from rest_framework import status
from rest_framework import pagination
from rest_framework.response import Response


class CustomResponse:
    @staticmethod
    def success(msg="Success", data=None, status_code=status.HTTP_200_OK):
        response_data = {"msg": msg, "status": True}
        if data is not None:
            response_data["data"] = data
        return Response(response_data, status=status_code)

    @staticmethod
    def created(msg="Created", data=None):
        response_data = {"msg": msg, "status": True}
        if data is not None:
            response_data["data"] = data
        return Response(response_data, status=status.HTTP_201_CREATED)

    @staticmethod
    def bad_request(msg="Bad request", errors=None):
        response_data = {"msg": msg, "status": False}
        if errors is not None:
            response_data["errors"] = errors
        return Response(response_data, status=status.HTTP_400_BAD_REQUEST)

    @staticmethod
    def get_request(data=None):
        response_data = {"status": True, "data": data}
        return Response(response_data, status=status.HTTP_200_OK)

    @staticmethod
    def not_found(msg="Not found", errors=None):
        response_data = {"msg": msg, "status": False}
        if errors is not None:
            response_data["errors"] = errors
        return Response(response_data, status=status.HTTP_404_NOT_FOUND)

    @staticmethod
    def unauthorized(msg="Unauthorized", errors=None):
        response_data = {"msg": msg, "status": False}
        if errors is not None:
            response_data["errors"] = errors
        return Response(response_data, status=status.HTTP_401_UNAUTHORIZED)

    @staticmethod
    def forbidden(msg="Forbidden", errors=None):
        response_data = {"msg": msg, "status": False}
        if errors is not None:
            response_data["errors"] = errors
        return Response(response_data, status=status.HTTP_403_FORBIDDEN)

    @staticmethod
    def server_error(msg="Server error", errors=None):
        response_data = {"msg": msg, "status": False}
        if errors is not None:
            response_data["errors"] = errors
        return Response(response_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)


class CustomPagination(pagination.PageNumberPagination):
    page_size = 10  # 每页显示的条目数
    page_size_query_param = 'page_size'
    max_page_size = 100

    def get_paginated_response(self, data):
        return Response({
            'count': self.page.paginator.count,
            'page_count': self.page.paginator.num_pages,
            'results': data
        })
